<?php
namespace App\Services;
use Exception,Log;
use App\Models\Linkslistsqrcodes;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use App\Models\Linkslistsminapps;
use App\Models\Linkslists;
use App\Services\HtmlossService;
use App\Models\Workqrcodeimg;

class CacheService
{
    public static function removeLinksCache($links_id)
    {
        // 删除短链接缓存数据
        Cache::forget('links_id_'.$links_id);
        Cache::forget('links_api_'.$links_id);
        Cache::forget('links_miniapp_'.$links_id); // 即将可以删除
        Cache::forget('links_html_'.$links_id); // 即将可以删除
        Cache::forget('links_htmls_'.$links_id);
        // 删除样式缓存
        Cache::forget('links_style_'.$links_id);
        Cache::forget('links_copy_'.$links_id);
        Cache::forget('links_share_'.$links_id);
        Cache::forget('links_spare_'.$links_id);
        // 删除当前的APPID
        Cache::forget('link_mini_'.$links_id);
        Cache::forget('links_card_'.$links_id);
        Cache::forget('link_card_'.$links_id."_dy");
    }

    public static function miniappCacheRemove($appid)
    {
        Cache::forget('links_token_'.$appid);
        Cache::forget('private_miniapp_'.$appid);
        Cache::forget('public_miniapp_'.$appid);
    }

    public static function removeLinksQrcodeCache($links_id,$batch="")
    {
        $batch = empty($batch) ? date("Ymd") : $batch;
        // 删除二维码缓存,需要提前更新数据库访问量
        $date = date("Ymd");
        // 超过2点系统已处理了无需查询
        if ($date != $batch && date("H") >= 2) return;
        $qrcodes = Redis::hget("links_qrcodes_".$batch, $links_id);
        if (empty($qrcodes)) return;
        $qrcodes = json_decode($qrcodes);
        foreach ($qrcodes as $qrcode) {
            // 如果是更新昨天的数据,则清空今日识别数量
            $scan_today = ($date != $batch) ? 0 : $qrcode->scan_today;
            // 更新数据库记录
            Linkslistsqrcodes::updateLinksListsQrcodesScan($qrcode->qrcodes_id,$qrcode->scan_total,$scan_today);
        }
        // 删除Redis内的数据
        Redis::hdel("links_qrcodes_".$batch, $links_id);
    }

    public static function removeWorkLinkQrcodeCache($kfid,$batch="")
    {
        $batch = empty($batch) ? date("Ymd") : $batch;
        // 删除二维码缓存,需要提前更新数据库访问量
        $date = date("Ymd");
        // 超过2点系统已处理了无需查询
        if ($date != $batch && date("H") >= 2) return;
        $qrcodes = Redis::hget("work_link_qrcode_".$batch, $kfid);
        if (empty($qrcodes)) return;
        $qrcodes = json_decode($qrcodes);
        foreach ($qrcodes as $qrcode) {
            // 如果是更新昨天的数据,则清空今日识别数量
            $scan_today = ($date != $batch) ? 0 : $qrcode->scan_today;
            // 更新数据库记录
            Workqrcodeimg::updateWorkQrcodeImgScan($qrcode->id,$qrcode->scan_total,$scan_today);
        }
        // 删除Redis内的数据
        Redis::hdel("work_link_qrcode_".$batch, $kfid);
    }

    public static function usersMiniappCacheRemove($users_id)
    {
        $userlinks = Linkslists::getUsersLinksSample($users_id);
        foreach ($userlinks as $userlink) {
            // 删除链接缓存数据
            self::removeLinksCache($userlink->links_id);
            HtmlossService::aliossHtmlDelete($userlink->links_id,$userlink->created_at);
        }
    }

}
